<template>
  <div class="top-container">
    <div class="top-top"></div>
    <div class="top-main">
      <div class="icon"></div>
      <div class="dividing-line"></div>
      <div class="main-title">迅航<br>News</div>
      <div class="sub-title">--武大新闻整合平台</div>
      <div class="main-search">
<!--        <div class="search-box">

        </div>
        <div class="search-icon" @click=""></div>-->
      </div>
    </div>
    <div class="top-background"></div>
  </div>
  <div class="login-container">
    <h1>管理员登录</h1>
    <form @submit.prevent="handleLogin">
      <div class="input-group">
        <input
            type="text"
            v-model="username"
            placeholder="请输入管理员账号"
            required
        >
      </div>

      <div class="input-group">
        <input
            type="password"
            v-model="password"
            placeholder="请输入密码"
            required
        >
      </div>
      <button type="submit">登录</button>
    </form>

    <div v-if="message" :class="['message', messageType]">
      {{ message }}
    </div>
  </div>
</template>
<script setup>
import { ref } from 'vue';
import router from "@/router/index.js";
import axios from 'axios';
import {useRoute} from "vue-router";

const username = ref('');
const password = ref('');
const error = ref('')
const message = ref('');
const messageType = ref('');
const route = useRoute()
// 简单模拟登录验证
const handleLogin = async() => {
  // 清除之前的状态
  message.value = '';
  const payload = {
    id: username.value,
    name: 1,
    password: password.value
  }
  // 简单验证逻辑（实际项目中应使用后端验证）
  try {
    const res = await axios.post('http://localhost:8080/admin/login', payload, {
      headers: {
        'Content-Type': 'application/json'
      }
    })


    if(res.data.success){
        await router.push({path:'/AdminPage',query:{
            token:res.data.data
          }}).then(() => {
          location.reload()  // 强制刷新
        })}
        else{
          alert( '登录失败：')
        }
  } catch (err) {
    error.value = err.response?.data?.message || '登录失败'
  }
  {
    const loadData = async (id) => {
      const res = await axios.get(`http://localhost:8080/api/news/${id}`)
      news.value = res.data
    }

// 初始化加载
    onMounted(() => {
      loadData(route.params.id)
    })
    watch(() => route.params.id, (newId) => {
      loadData(newId)
    })
  }
};
</script>
<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  font-size: 16px; /* 基准字体大小 */
}

body {
  font-family: Arial, sans-serif;
  background-color: #f5f5f5;
  color: #0e0d0d;
  line-height: 1.6;
  min-width: 320px;
  overflow-x: hidden;
}
.top-container {
  width: 100%;
  max-width: 120rem; /* 1920px/16 */
  margin: 0 auto;
  padding: 0;
}

.top-top {
  height: 3.125rem; /* 50px/16 */
  background-color: #02A7F0;
  opacity: 0.5;
  border: 1px solid #02A7F0;
}

.top-main {
  display: flex;
  padding: 0.9375rem 6.25rem; /* 15px/16, 100px/16 */
  height: 6.25rem; /* 100px/16 */
  background-color: white;
  align-items: center;
  justify-content: space-between;
}

.top-background {
  position: relative;
  width: 100%;
  height: 12.5rem; /* 200px/16 */
  background-color: rgba(2, 167, 240, 0.34);
  border: 1px solid #797979;
}

.top-background::after {
  position: absolute;
  content: "";
  width: 100%;
  height: 100%;

  opacity: 0.2;
  background-size: cover;
  overflow: hidden;
  border: 1px solid #797979;
  z-index: 1;
}

.icon {
  width: 4.375rem; /* 70px/16 */
  height: 4.375rem; /* 70px/16 */
  background-image: url("@/assets/whu_logo.ico");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  flex-shrink: 0;
}

.dividing-line {
  margin: 0.3125rem 0.9375rem; /* 5px/16, 15px/16 */
  height: 3.75rem; /* 60px/16 */
  width: 1px;
  border: 1px solid #797979;
  flex-shrink: 0;
}

.main-title {
  margin: 0.3125rem 0.5625rem; /* 5px/16, 9px/16 */
  width: 5rem; /* 80px/16 */
  height: 3.75rem; /* 60px/16 */
  font-size: 1.5625rem; /* 25px/16 */
  line-height: 1.8125rem; /* 29px/16 */
  flex-shrink: 0;
}

.sub-title {
  width: 18.75rem; /* 300px/16 */
  height: 2.5rem; /* 40px/16 */
  padding-top: 0.5rem; /* 8px/16 */
  margin-top: 1.875rem; /* 30px/16 */
  margin-left: -1rem; /* -16px/16 */
  color: #8080FF;
  opacity: 0.86;
  font-size: 0.9375rem; /* 15px/16 */
  flex-shrink: 0;
}

.main-search {
  display: flex;
  width: 21.875rem; /* 350px/16 */
  height: 3.125rem; /* 50px/16 */
  margin-left: auto;
  margin-right: 2rem;
  margin-top: 0.625rem; /* 10px/16 */


  border-radius: 2.5rem; /* 40px/16 */
}

.search-box {
  width: 100%;
  height: 100%;
  padding: 0.625rem 1.875rem; /* 10px/16, 30px/16 */
  color: #555555;
  font-size: 0.9375rem; /* 15px/16 */
  opacity: 0.8;
}

.search-icon {
  margin-top: 0.625rem; /* 10px/16 */
  margin-right: 1.125rem; /* 18px/16 */
  height: 1.875rem; /* 30px/16 */
  width: 1.875rem; /* 30px/16 */
  background: url('@/assets/搜索.png') no-repeat center;
  background-size: cover;
  cursor: pointer;
}

.search-icon:hover {
  background-color: rgba(43, 84, 131, 0.1);
}

.custom-input >>> .el-input__wrapper {
  border: none;
  box-shadow: none;
  background-color: transparent;
}

.custom-input >>> input {
  background-color: transparent;
  color: #333;
}
.login-container {
  max-width: 400px;
  margin: 50px auto;
  padding: 20px;
  border: 1px solid #ddd;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
  text-align: center;
  color: #333;
}
.input-group {
  margin-bottom: 15px;
}

label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
}

input {
  height:45px;
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  margin-bottom: 30px;

}

button {
  width: 100%;
  padding: 12px;
  background-color: #7baee1;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

button:hover {
  background-color: #34495e;
}

.message {
  margin-top: 15px;
  padding: 10px;
  border-radius: 4px;
  text-align: center;
}
</style>